/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.welfare.board;

import java.util.*;

import java.io.File;
import javax.servlet.*;
import javax.servlet.http.*;

import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;




/**
 *
 * @author eastar
 */
public class WordReplyCommand implements Command
{
    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	HttpSession session = (HttpSession)request.getSession();
    	String userId 		= StringUtil.nvl(session.getAttribute("session.user_id"));
    	String userAuth 	= StringUtil.nvl(session.getAttribute("session.user_auth"));

    	HashBox box = new HashBox(request);

		BoardDAO boardDAO = new BoardDAO();


		//게시판 쓰기권한 체크
		HashList wordInfo		= boardDAO.selectWordInfo(box.getParam());
		String writeAuth		= wordInfo.get("write_auth",0);

		if ("A".equals(writeAuth)) {
			if ("S".equals(userAuth) || "A".equals(userAuth)) {
				//admin pass
			} else {
				throw new Exception("쓰기권한이 없습니다.");
			}
		} else if ("M".equals(writeAuth)) {
			if ("".equals(userId)) {
				throw new Exception("로그인 후 사용가능합니다.");
			}
		}


		//글저장
		String boardId = box.get("board_id");
		
		box.put("job_type", "R");
		box.put("word_num", wordInfo.get("word_num",0));
		box.put("ip_addr",	request.getRemoteAddr());
		box.put("sort",    	boardDAO.selectWordSort((HashMap)wordInfo.get(0)));
		box.put("depth",    wordInfo.getInt("depth",0)+1);
		box.put("content", 	box.get("content_"+boardId));
		box.put("html_yn", 	wordInfo.get("editor_yn",0));

		boardDAO.saveWord(box.getParam());


		//결과 페이지 이동
		HashMap result		= new HashMap();
		result.put("result", 	"true");
		result.put("action",	"/board/wordList.jsp?board_id="+boardId);

    	request.setAttribute("result", result);

    	return "/common/move_view.jsp";
    }
}
